package com.chang.cheng.toutiaoserver.login.serviceV1_1.impl;

import com.chang.cheng.toutiaoserver.login.mapper.ApArticleMapper;
import com.chang.cheng.toutiaoserver.login.serviceV1_1.ApUserFindArticlesService;
import com.chang.cheng.toutiaoserver.model.common.dtos.ResponseResult;
import com.chang.cheng.toutiaoserver.model.user.pojos.Article;
import com.chang.cheng.toutiaoserver.utils.LocalDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: asus
 * @Date: 2022/01/07 10:03
 * @Description:
 */
@Service
public class ApUserFindArticlesServiceImpl implements ApUserFindArticlesService {
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Override
    public ResponseResult findArticles(Long channel_id,Long timestamp) {
        //将时间戳转为字符串
        String startDate = LocalDateUtil.stampToString(timestamp);
//        System.out.println("测试进入时间:"+startDate);

        Map<String, Object> map = new HashMap<>();
        List<Article> articles = apArticleMapper.findArticles(channel_id, startDate);
        int size = articles.size();
        Long  pre_timestamp=timestamp;
        if(size>0){
            String created_timeS = articles.get(size - 1).getPubdate();
//            System.out.println("测试输出时间:"+created_timeS);
            Long created_timeL = LocalDateUtil.stringTOTimeStamp(created_timeS);
            // 因为刚进来要减去实际时间和测试时间差的,所以要加上.
            pre_timestamp=created_timeL;
        }
        map.put("pre_timestamp", pre_timestamp);
        map.put("results", articles);
        return ResponseResult.okResult(map);
    }
}
